﻿using PGMHelper;
using System;
using System.Collections.Generic;
using System.Windows.Forms.DataVisualization.Charting;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GBCodeSpecturmDemo
{
    /// <summary>
    /// 主窗体
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 反应谱对象
        /// </summary>
        private GBCodeSpectrumSet Spectrums { set; get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            //初始化
            this.Spectrums = new GBCodeSpectrumSet();
            //设定外观
            this.TabControl.SetApperance();
            this.SpectrumDataGridView.SetApperance(DataGridViewSelectionMode.FullColumnSelect);
            this.SpecturmChart.SetApperance(StringAlignment.Near, true);
        }

        /// <summary>
        /// 增加反应谱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshCodeSpectrumCalButton_Click(object sender, EventArgs e)
        {
            //添加反应谱
            this.Spectrums.Add(new List<TextBox>
            {
                this.AfaMaxTextbox,
                this.TgTextbox,
                this.DampRatioTextbox,
            });
            //列表为空直接返回
            if (this.Spectrums.Count == 0) return;
            //数据绑定
            this.SpectrumCheckListbox.SetDataSource(this.Spectrums.SpectrumList, "Descp", "Descp");
        }

        /// <summary>
        /// 清空按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearButton_Click(object sender, EventArgs e)
        {
            //数据绑定
            this.SpectrumCheckListbox.SetDataSource(new List<GBCodeSpectrum>(), "Descp", "Descp");
            //绘图
            this.SpectrumCheckListbox_SelectedIndexChanged(sender, e);
        }

        /// <summary>
        /// 选择项改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SpectrumCheckListbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获得勾选的对象
            var spectrumObjs = this.SpectrumCheckListbox.GetObjects().ConvertAll(s => (GBCodeSpectrum)s);
            //获得数据
            var data = this.Spectrums.GetData(spectrumObjs);
            //修改表格
            this.SpectrumDataGridView.AddDatas(data);
            //绘图
            this.Spectrums.ToChart(this.SpecturmChart, spectrumObjs);
        }
    }
}
